<?php
/**
 * NOTE: This class requires PHP 5.3.
 */

/**
 * Description of Core\String
 *
 * @author erlingowe
 */

namespace Core;

class String extends ArrayObject {

    private $str = "";


    function __construct( $str ) {
        $this->str = $str;
    }

    //public function isBBCode () {

    //}

    /**
     * Splits a string into a Core\StringCollection.
     * 
     * @param string $strExpression
     * @param integer $intLimit
     * @param integer $intStart
     * @return Core\StringCollection
     */
    public function split ( $strExpression, $intLimit = 0, $intStart = 0 ) {

    }

    /**
     * Checks if the string is an e-mail address.
     *
     * @return boolean
     */
    public function isEmail ( $str = null ) {

        if ( $this ) {
            $str = $this->str;
        } elseif ( !$this and !$str ) {
            throw new \Exception ( "\$str has to be set when Core\String::isEmail() is called with SRO." );
        }

      // ToDo: Rewrite the end so that only two, three or four character top
      // domains are allowed, preferably only the actual topdomains. If
      // possible, check if domain actually exists.

        return ( \pregmatch( "/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-])+$/", $this->str ) ) ? true : false;

    }

    public function match ( $strExpression ) {

    }

    /**
     * Replaces a substring or a regular expression pattern in a string.
     *
     * @param string $strExpression The substring or regular expression pattern to be replaced.
     * @param string $strInsert The string to insert instead.
     * @param integer $intLimit
     * @param integer $intStart
     * @return Core_String
     */
    public function replace ( $strExpression, $strInsert = "", $intLimit = 0, $intStart = 0 ) {

    }

    /**
     *
     * @return Core_String 
     */
    public function trim () {
        // ToDo: Enable second parameter for trim(). <-- ???????
        return new String ( \trim ( $this->str ) );
    }

}


// Example of use:

use Core;

$strEmail = " foo@bar.org     ";

// Alternative 1
$cstrString = new String ( $strEmail );
if ( $cstrString->trim()->isEmail() ) echo "yepp";

// Alternative 2
if ( String::trim( $strEmail )->isEmail() ) echo "yepp";

?>
